package cn.hit.thread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.swing.JProgressBar;
import javax.swing.JTextArea;

import cn.hit.constant.HitConstants;
import cn.hit.utils.HitUtils;

public class ExecuteExportFileThread extends Thread {

    private List<File> files;
    private JTextArea logArea;
    private JProgressBar pbar;
    private StringBuilder failLog;
    private String targetPath;
    private StringBuilder exportLog;

    public ExecuteExportFileThread(List<File> files, JTextArea logArea, JProgressBar pbar,
            String targetPath) {
        this.files = files;
        this.logArea = logArea;
        this.pbar = pbar;
        this.failLog = new StringBuilder();
        this.targetPath = targetPath;
        this.exportLog = new StringBuilder();
    }

    @SuppressWarnings("static-access")
    @Override
    public void run() {
        int successCount = 0;
        int failCount = 0;
        pbar.setMaximum(files.size());
        pbar.setVisible(true);
        File exportFile;
        InputStream ips = null;
        OutputStream ops = null;
        byte[] buffer;
        try {
            logArea.setText("");
            String path = null;
            for (File file : files) {
                if (file.exists()) {
                    exportLog.append(file.getAbsolutePath());
                    String printedText = null;
                    if (successCount > HitConstants.LOG_ROLL_LINE_SIZE) {
                        printedText = logArea.getText()
                                .substring(logArea.getText().indexOf("......") + 7,
                                        logArea.getText().length());
                    } else {
                        printedText = logArea.getText();
                    }
                    path = file.getAbsolutePath();
                    logArea
                            .setText(printedText + "正在执行导出脚本文件："
                                    + path.substring(path.lastIndexOf("/") + 1, path.length())
                                    + "......\n");
                    try {
                        String tmpPath = file.getAbsolutePath().substring(
                                file.getAbsolutePath().indexOf("db"),
                                file.getAbsolutePath().lastIndexOf("\\"));
                        File mkdir = new File(targetPath + "\\"
                                + HitConstants.EXPORT_SOURCE_DRICTORY + "\\" + tmpPath);
                        if (!mkdir.exists()) {
                            mkdir.mkdirs();
                        }
                        exportFile = new File(mkdir.getAbsolutePath() + "\\" + file.getName());
                        ops = new FileOutputStream(exportFile);
                    } catch (Exception e) {
                        logArea
                                .setText("************************执行脚本列表路径非法************************");
                        e.printStackTrace();
                        return;
                    }

                    try {
                        ips = new FileInputStream(file);
                        buffer = new byte[ips.available()];
                        ips.read(buffer);
                        ops.write(buffer);
                    } catch (Exception e) {
                        failLog.append("失败导出脚本文件:" + file.getAbsolutePath() + "\n");
                        failCount++;
                        e.printStackTrace();
                        exportLog.append("**********导出脚本失败\n\t" + e.toString());
                    } finally {
                        try {
                            ips.close();
                            ops.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    successCount++;
                    exportLog.append("**********导出脚本完成\n");
                } else {
                    exportLog.append("\n\t" + file.getAbsolutePath());
                    exportLog.append("**********文件不存在\n\n");
                    failCount++;
                    failLog.append("失败导出脚本文件数量:" + failCount + "\n失败导出脚本文件:"
                            + file.getAbsolutePath() + "\n");
                }
                pbar.setValue(successCount + failCount);
                try {
                    this.sleep(HitConstants.DEFER_SLEEP_TIME);
                } catch (InterruptedException e) {
                    logArea.setText("************************非正常结束************************");
                    exportLog.append(logArea.getText());
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logArea.setText("************************非正常结束************************");
            exportLog.append(logArea.getText());
            e.printStackTrace();
        }
        if (successCount <= 0) {
            logArea.setText("************************非法脚本文件************************");
        } else {
            logArea.setText("************************执行导出脚本文件完成************************\n"
                    + "成功导出脚本文件数量:" + successCount + "\n" + failLog.toString());
        }
        pbar.setVisible(false);
        HitUtils.exportLogFile(targetPath, "导出sql脚本日志", exportLog.toString());
    }

}
